/***************服务器端API及客户端请求技术的二次封装模块******************/
// 服务器域名基础地址
export let base = 'https://web.codeboy.com/mfresh/'

// POST请求的公共选项
let postOptions={
  method:'POST',
  headers:{   //请求头部
    'Content-Type':'application/x-www-form-urlencoded'
  },
}

/**
* 1.1API-向购物车中添加商品
*请求参数：
  pid-产品ID，必需
*请求头部:
   token
*输出结果：
* {"code":1,"msg":"succ", "productCount":1}
* 或
* {"code":400}
*/
export let cartDetailAdd= async(pid)=>{
  let url = base+'data/cart_detail_add.php'
  let token = localStorage['userToken']
  let res= await fetch(url,{
    ...postOptions,
    headers:{   //请求头部
      'Content-Type':'application/x-www-form-urlencoded',
      'authorization':token      //通过请求头方式向服务器提交身份令牌
    },
    body:`pid=${pid}`
  })
  let data = await res.json()
  return data
}

/**
* 1.2根据购物车详情记录编号删除该购买记录
*请求参数：
  did-详情记录编号
*输出结果：
* {"code":1,"msg":"succ"}
* 或
* {"code":400}
*/

export let cartDetailDelete= async(did)=>{
  let url = base+'data/cart_detail_delete.php'
  let token = localStorage['userToken']
  let res= await fetch(url,{
    ...postOptions,
    headers:{   //请求头部
      'Content-Type':'application/x-www-form-urlencoded',
      'authorization':token      //通过请求头方式向服务器提交身份令牌
    },
    body:`did=${did}`
  })
  let data = await res.json()
  return data
}

/**
*1.3查询指定用户的购物车内容
*请求参数：
  uid-用户ID，必需
*输出结果：
  {
    "uid": 1,
    "products":[
      {"pid":1,"title1":"xxx","pic":"xxx","price":1599.00,"count":3},
      {"pid":3,"title1":"xxx","pic":"xxx","price":1599.00,"count":3},
      ...
      {"pid":5,"title1":"xxx","pic":"xxx","price":1599.00,"count":3}
    ]
  }
*/
export let cartDetailSelecte= async(uid)=>{
  let url = base+'data/cart_detail_select.php'
  let token = localStorage['userToken']
  let res= await fetch(url,{
    ...postOptions,
    headers:{   //请求头部
      'Content-Type':'application/x-www-form-urlencoded',
      'authorization':token      //通过请求头方式向服务器提交身份令牌
    },
    body:`uid=${uid}`
  })
  let data = await res.json()
  return data
}

/**
*1.4根据购物车详情记录编号修改该商品购买数量
*请求参数：
  did-详情记录编号
  pid-商品编号
  count-更新后的购买数量
*输出结果：
* {"code":1,"msg":"succ"}
* 或
* {"code":400}
*/

export let cartDetailUpdate= async(did,pid,count)=>{
  let url = base+'data/cart_detail_update.php'
  let token = localStorage['userToken']
  let res= await fetch(url,{
    ...postOptions,
    headers:{   //请求头部
      'Content-Type':'application/x-www-form-urlencoded',
      'authorization':token      //通过请求头方式向服务器提交身份令牌
    },
    body:`did=${did}&pid=${pid}&count=${count}`
  })
  let data = await res.json()
  return data
}

//接口封装
/**
 * 2.1API
 *根据新闻ID返回新闻详情
 *请求参数：
 *nid-新闻ID，必需
 *输出结果：
  {
    "nid": 1,
    ...
  }
 */
export let newsDetail = async(nid)=>{
  let url = base+'data/news_detail.php?nid=' + nid
  let res = await fetch(url)
  let data = await res.json()
  return data
}

/** 2.2
*按发布时间逆序返回新闻列表
*请求参数：
  pageNum-需显示的页号；默认为1
  pageSize-页面大小,一次最多可以返回的新闻条数:默认为6
*输出结果：
  {
    totalRecord: 58,
    pageSize: 10,
    pageCount: 6,
    pageNum: 1,
    data: [{},{} ... {}]
  }
*/
export let newsSelect = async(pageNum,pageSize)=>{
  let url = base+`data/news_select.php?pageSize=${pageSize}pageNum=${pageNum}`
  let res = await fetch(url)
  let data = await res.json()
  return data
}



/**
*3.1根据产品ID返回产品详情
*请求参数：
  pid-产品ID，必需
*输出结果：
  {
    "pid": 1,
    "title1":"xxx",
    ...
  }
*/
export let productDetail = async(pid)=>{
  let url = base+'data/product_detail.php?phone=' + pid
  let res = await fetch(url)
  let data = await res.json()
  return data
}

/**
*3.2分页获取产品信息
*请求参数：
  pageNum-需显示的页号；默认为1
  type-可选，默认为1
*输出结果：
  {
    totalRecord: 37,
    pageSize: 6,
    pageCount: 7,
    pageNum: 1,
    type: 1,
    data: [{},{} ... {}]
  }
*/
export let productSelect = async(pageNum)=>{
  let url = base+'data/product_select.php?pageNum=' + pageNum
  let res = await fetch(url)
  let data = await res.json()
  return data
}

/**4.1
*验证电话号码是否已经存在
*请求参数：
  phone-用户名
*输出结果：
* {"code":1,"msg":"exist"}
* 或
* {"code":2,"msg":"non-exist"}
*/
export let userCheckPhone = async(phone)=>{
  let url = base+'data/user_check_phone.php?phone=' + phone
  let res = await fetch(url)
  let data = await res.json()
  return data
}

/**4.2
*验证用户名是否已经存在
*请求参数：
  uname-用户名
*输出结果：
* {"code":1,"msg":"exist"}  存在
* 或
* {"code":2,"msg":"non-exist"}  不存在
*/
export let user_check_uname = async(uname)=>{
  let url = base+'data/user_check_uname.php?uname=' + uname
  let res = await fetch(url)
  let data = await res.json()
  return data
}

/**
 * 4.3:用户名和密码
 *用户登录验证
 *请求参数：
  unameOrPhone-用户名或密码
  upwd-密码
 *输出结果：
 * {"code":1,"uid":1,"uname":"test","phone":"13012345678"}
 * 或
 * {"code":400}
 */
 export let user_login = async(unameOrPhone,upwd)=>{
  let url = base+`data/user_login.php`
  let res = await fetch(url,{
    ...postOptions,
    body:`unameOrPhone=${unameOrPhone}&upwd=${upwd}`
  })
  let data = await res.json()
  return data
}
/**
 * 4.4:
 *注册新用户
 *请求参数：
  uname-用户名
  upwd-密码
  phone-电话号码
 *输出结果：
 * {"code":1,"uid":3,"uname":"test"}
 * 或
 * {"code":500}
*/
export let user_register = async(uname,upwd,phone)=>{
  let url = base+`data/user_register.php`
  let res = await fetch(url,{
    ...postOptions,
    body:`uname=${uname}&upwd=${upwd}&phone=${phone}`
  })
  let data = await res.json()
  return data
}
/**
 * API-5.1获得录播广告数据
*/
export let carouselSelect=async()=>{
  let url =base+'data/carousel_select.php'
  let res=await fetch(url)
  let data=await res.json()
  return data
}